<?php

class MAuthClass extends baseModel {
	
	private $lsTableUser;

	public function __construct() {
		parent::__construct();
		global $_config;
		$this->lsTableUser = $_config["connection"]["tableprefix"]."user";
	}

	public function UserLogin($isUsername, $isPassword) {
		$result = $this->SimpleQuery("SELECT level FROM ".$this->lsTableUser." WHERE Valid = 'Y' AND username = '".$isUsername."' AND password = '".$isPassword."' AND Valid = 'Y'");	
                
		if(!empty($result)) {
			return $result[0]; // vratit pouze prvni radek
		} else {
			return null;
		}
	}

	public function EmailLogin($isEmail, $isPassword) {
		$result = $this->SimpleQuery("SELECT level, username, iduser FROM ".$this->lsTableUser." WHERE Valid = 'Y' AND email = '".$isEmail."' AND password = '".$isPassword."' AND Valid = 'Y'");	
                echo "login?";
                print_r($result);
		if(!empty($result)) {
			return $result[0]; // vratit pouze prvni radek
		} else {
			return null;
		}
	}

	public function newUser($irNewUser) {
		$lsResult = "OK";
		// nejprve zjistit, pokud neexistuje uzivatel se stejnym jmenem nebo e-mailem
		if(!empty($irNewUser["username"])) {
			$lnCount = $this->RowCount("SELECT username FROM ".$this->lsTableUser." WHERE username = '".$irNewUser["username"]."' AND Valid = 'Y'");
			if($lnCount > 0) {
				$lsResult = "USEREXISTS";
			}
		}
		if(!empty($irNewUser["email"])) {
			$lnCount = $this->RowCount("SELECT email FROM ".$this->lsTableUser." WHERE email = '".$irNewUser["email"]."' AND Valid = 'Y'");
			if($lnCount > 0) {
				$lsResult = "EMAILEXISTS";
			}
		}
		//
		if($lsResult=="OK") {
			$lsSQL = "INSERT INTO ".$this->lsTableUser." (username,
														  email,
														  password,
														  ValidTo)
												  VALUES('".$irNewUser["username"]."',
												         '".$irNewUser["email"]."',
														 '".$irNewUser["password"]."',
														 '".getMaxTime()."')";
			try {
				$this->SimpleQuery($lsSQL, DATABASE_QUERY_TYPE_INSERT);
			} catch (Exception $e) {
				$lsResult = "ERROR";
			}
			
		}
		return $lsResult;
	}

}

?>